package com.xxx.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.vo.CourseTypeCrumbsVo;
import com.xxx.service.ICourseTypeService;
import com.xxx.domain.CourseType;
import com.xxx.query.CourseTypeQuery;
import com.xxx.result.JSONResult;
import com.xxx.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;


@RestController
@RequestMapping("/courseType")
public class CourseTypeController {

    @Autowired
    public ICourseTypeService courseTypeService;



    /**
     * 获取类型面包屑
     */
    @RequestMapping(value = "/crumbs/{courseTypeId}", method = RequestMethod.GET)
    public JSONResult getCourseTypeCrumbs(@PathVariable("courseTypeId") Long courseTypeId) {
        List<CourseTypeCrumbsVo> vos = courseTypeService.getCourseTypeCrumbs(courseTypeId);
        return JSONResult.success(vos);
    }



    /**
     * 获取类型树
     */
    @RequestMapping(value = "/treeData", method = RequestMethod.GET)
    public JSONResult treeData() {
        return JSONResult.success(courseTypeService.treeData());
    }

    /**
     * 根据父ID查询子类型
     */
    @RequestMapping(value = "/selectChildrenById/{pid}", method = RequestMethod.GET)
    public JSONResult selectChildrenById(@PathVariable("pid") Long pid) {
        List<CourseType> children = courseTypeService.selectList(new com.baomidou.mybatisplus.mapper.EntityWrapper<CourseType>().eq("pid", pid));
        return JSONResult.success(new PageList<CourseType>((long) children.size(), children));
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public JSONResult batchDelete(@RequestBody List<Long> ids) {
        courseTypeService.deleteBatchIds(ids);
        return JSONResult.success();
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CourseType courseType) {
        if (courseType.getId() != null) {
            courseTypeService.updateById(courseType);
        } else {
            courseTypeService.insert(courseType);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseTypeService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseTypeService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(courseTypeService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseTypeQuery query) {
        Page<CourseType> page = new Page<CourseType>(query.getPage(), query.getRows());
        // 添加根据pid查询的条件
        EntityWrapper<CourseType> wrapper = new EntityWrapper<>();
        if (query.getPid() != null) {
            wrapper.eq("pid", query.getPid());
        }
        if (query.getKeyword() != null && !query.getKeyword().trim().isEmpty()) {
            wrapper.like("name", query.getKeyword().trim());
        }
        page = courseTypeService.selectPage(page, wrapper);
        return JSONResult.success(new PageList<CourseType>(page.getTotal(), page.getRecords()));
    }
}
